import showMsg from "@/utils/message/message"
import YXRequest from "./request/request"
import { userStore } from '@/store/user'

let errMsg = ''

export const yxRequest = new YXRequest({
  baseURL:  '/api',
  timeout: 10000,
  interceptor: {
    reqInterceptor: (config) => {
      return config
    },
    resInterceptor: (res) => {
      return res
    },
    resInterceptorCache(err) {
      // if (errMsg === err.response.data.message) return  // 两次错误提示信息一样时不重复显示
      errMsg = err.response.data.message
      showMsg('error', errMsg ? errMsg : err.message, 2000) // 拦截无权限访问的请求
      if (errMsg === '匿名用户没有此权限') {
        setTimeout(() => {
          const user = userStore()
          user.errorLogout()
        }, 2000)
      }
    },
  }
})
